/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddPatientWindow.java
 *
 * Created on 08 2, 11, 7:40:19 PM
 */

package dcds2;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
import java.sql.*;
/**
 *
 * @author Mesan
 */
public class AddPatientWindow extends javax.swing.JFrame {

    public int totalPatient = 0;
    int patientID;
    public String[] months = {"January","February", "March","April", "May", "June", "July", "August", "September"
                        , "October", "November", "December"};
    public String[] days31 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", 
    "18", "19", "20", "21", "22", "23", "24", "25", "26","27","28","29", "30", "31"};
    
    public String[] days30 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", 
    "18", "19", "20", "21", "22", "23", "24", "25", "26","27","28","29", "30"};
    public String[] days28 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", 
    "18", "19", "20", "21", "22", "23", "24", "25", "26","27","28"};
    public String[] days29 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", 
    "18", "19", "20", "21", "22", "23", "24", "25", "26","27","28","29"};
    
    /** Creates new form AddPatientWindow */
    public AddPatientWindow() {
        initComponents();
        setLocationRelativeTo(null);
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        groupGender = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtFirstName = new javax.swing.JTextField();
        txtMiddleName = new javax.swing.JTextField();
        txtLastName = new javax.swing.JTextField();
        txtContactNo = new javax.swing.JTextField();
        comboMonth = new javax.swing.JComboBox();
        txtYear = new javax.swing.JTextField();
        comboDay = new javax.swing.JComboBox();
        radiobtnMale = new javax.swing.JRadioButton();
        radiobtnFemale = new javax.swing.JRadioButton();
        comboCivil = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        btnConfirm = new javax.swing.JButton();
        btnBack = new javax.swing.JButton();
        txtAddress = new javax.swing.JTextField();

        groupGender.add(radiobtnMale);
        groupGender.add(radiobtnFemale);

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("DCDS - Add Patient");
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));

        jLabel1.setFont(new java.awt.Font("Century", 1, 12));
        jLabel1.setText("PERSONAL INFORMATION");

        jLabel2.setFont(new java.awt.Font("Century", 0, 12));
        jLabel2.setText("First Name:");

        txtFirstName.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));

        comboMonth.setMaximumRowCount(12);
        comboMonth.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }));
        comboMonth.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboMonthActionPerformed(evt);
            }
        });

        txtYear.setText("<year>");
        txtYear.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtYearMouseClicked(evt);
            }
        });
        txtYear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtYearActionPerformed(evt);
            }
        });
        txtYear.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtYearFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtYearFocusLost(evt);
            }
        });
        txtYear.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                txtYearInputMethodTextChanged(evt);
            }
        });
        txtYear.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                txtYearPropertyChange(evt);
            }
        });

        comboDay.setMaximumRowCount(10);
        comboDay.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));

        radiobtnMale.setBackground(new java.awt.Color(204, 204, 204));
        radiobtnMale.setSelected(true);
        radiobtnMale.setText("Male");
        radiobtnMale.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radiobtnMaleActionPerformed(evt);
            }
        });

        radiobtnFemale.setBackground(new java.awt.Color(204, 204, 204));
        radiobtnFemale.setText("Female");
        radiobtnFemale.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radiobtnFemaleActionPerformed(evt);
            }
        });

        comboCivil.setMaximumRowCount(5);
        comboCivil.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Single", "Married" }));

        jLabel9.setFont(new java.awt.Font("Century", 0, 12));
        jLabel9.setText("Civil Status:");

        jLabel8.setFont(new java.awt.Font("Century", 0, 12));
        jLabel8.setText("Gender:");

        jLabel7.setFont(new java.awt.Font("Century", 0, 12));
        jLabel7.setText("Birthdate:");

        jLabel6.setFont(new java.awt.Font("Century", 0, 12));
        jLabel6.setText("Contact No.:");

        jLabel5.setFont(new java.awt.Font("Century", 0, 12));
        jLabel5.setText("Address:");

        jLabel4.setFont(new java.awt.Font("Century", 0, 12));
        jLabel4.setText("Last Name:");

        jLabel3.setFont(new java.awt.Font("Century", 0, 12));
        jLabel3.setText("Middle Name:");

        btnConfirm.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/module/ButtonConfirm.png"))); // NOI18N
        btnConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfirmActionPerformed(evt);
            }
        });

        btnBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/module/ButtonBack.png"))); // NOI18N
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 183, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel8)
                                    .addComponent(jLabel9)
                                    .addComponent(jLabel2))
                                .addGap(28, 28, 28)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtAddress, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
                                    .addComponent(txtFirstName, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
                                    .addComponent(txtLastName, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
                                    .addComponent(txtMiddleName, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
                                    .addComponent(txtContactNo, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                    .addComponent(comboCivil, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                                        .addComponent(radiobtnMale)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(radiobtnFemale)))
                                                .addGap(145, 145, 145))
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(txtYear, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(comboMonth, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(comboDay, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addGap(8, 8, 8)))
                                .addGap(14, 14, 14)))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(btnConfirm, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnBack, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtMiddleName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addComponent(jLabel5))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtContactNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(radiobtnMale, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(radiobtnFemale)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(comboCivil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnBack, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnConfirm, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
 

    private void radiobtnMaleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radiobtnMaleActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_radiobtnMaleActionPerformed

    private void radiobtnFemaleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radiobtnFemaleActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_radiobtnFemaleActionPerformed

    private void txtYearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtYearActionPerformed
   
        int year = 0;
        if(txtYear.getText().compareTo("<year>")==0){
            JFrame frame = new JFrame();
            JOptionPane.showMessageDialog(frame, "Please input year first", "NOTICE!", JOptionPane.PLAIN_MESSAGE);
        }else year = Integer.parseInt(txtYear.getText());
        if(year%4==0 && (year%100!=0 || year%400==0) )
            comboDay.setModel(new DefaultComboBoxModel(days29));
       else comboDay.setModel(new DefaultComboBoxModel(days28));
    
    }//GEN-LAST:event_txtYearActionPerformed

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
       dispose();
       new MainWindow(1).setVisible(true);
    }//GEN-LAST:event_btnBackActionPerformed

    private void btnConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfirmActionPerformed
       JFrame frame = new JFrame();
         int selection = JOptionPane.showConfirmDialog(frame, "Are all the fields correct?", "Confirmation", JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE);
         if(selection==JOptionPane.YES_OPTION){
              int errCtr = 0;
        
              String firstName = "\"" + txtFirstName.getText() + "\"" ;
              String lastName = "\"" + txtLastName.getText() + "\"" ;
              String middleName = "\"" + txtMiddleName.getText() + "\"" ;
              String address = "\"" + txtAddress.getText() + "\"";
             
              String birthMonth = "\"" + comboMonth.getSelectedItem().toString() +"\"";
              String civil = "\"" + comboCivil.getSelectedItem().toString() + "\"";
               
              String contactNo = "\""+txtContactNo.getText()+"\"";
              String birthYear = "\""+txtYear.getText()+"\"";
             
              //Check if Contact Number is a number
              try{
              long cn = Long.parseLong(txtContactNo.getText());
              }
              catch (NumberFormatException nfe){
                  errCtr++;
                JOptionPane.showMessageDialog(frame, "Please enter a valid contact number!","NOTICE!",JOptionPane.PLAIN_MESSAGE);  
              }
              
              //Check if Year is a number
              try{
              int by = Integer.parseInt(txtYear.getText());           
                
                }
              catch (NumberFormatException nfe){
                  errCtr++;
              JOptionPane.showMessageDialog(frame, "Please enter a number for the year!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
                }
           
              int birthDay = Integer.parseInt(comboDay.getSelectedItem().toString());
             
              String gender = "";
              
               if(radiobtnMale.isSelected() == true)
                    gender = "\""+"Male"+"\"";
                 
               else gender = "\""+"Female"+"\"";
               
               int patientID = 1;
               int month = comboMonth.getSelectedIndex()+1;
               int day = comboDay.getSelectedIndex() + 1; 
               
               int x =   patientCheck(firstName, lastName, middleName, address, birthMonth, civil, contactNo, birthYear, birthDay, gender);
               
               errCtr+=x; System.out.println("ERROR NAME " + errCtr);
                //====ERROR CHECKING====//

              if(txtFirstName.getText().length() == 0){
                  errCtr++;
                   JOptionPane.showMessageDialog(frame, "Please enter first name!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
                }
              
              else if(txtLastName.getText().length() == 0){
                  errCtr++;
                   JOptionPane.showMessageDialog(frame, "Please enter last name!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
                }
              
              else if(txtMiddleName.getText().length() == 0){
                  errCtr++;
                   JOptionPane.showMessageDialog(frame, "Please enter middle name!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
                }
              
              else if(txtAddress.getText().length() == 0){
                  errCtr++;
                   JOptionPane.showMessageDialog(frame, "Please enter address!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
                }
              
              else if(txtContactNo.getText().length() == 0){
                  errCtr++;
                   JOptionPane.showMessageDialog(frame, "Please enter a valid contact number!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
                }  
              else if(txtYear.getText().length() == 0 || txtYear.getText().length() != 4){
                      errCtr++;
                   JOptionPane.showMessageDialog(frame, "Please enter a valid year!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
                
              } else if(month> Integer.parseInt(month1.format(now)) && day> Integer.parseInt(day1.format(now)) 
                      && (Integer.parseInt(txtYear.getText()) > Integer.parseInt(year1.format(now)))){
                  errCtr++;
                   JOptionPane.showMessageDialog(frame, "Please enter a valid date!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
              }

              //====ERROR CHECKING====//
              
              if (errCtr == 0){
              try{
                           
              ConnectionFactory myFactory = ConnectionFactory.getFactory();
              Connection conn = myFactory.getConnection();
 
              boolean result = false;
            
              PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM dcds.patient");
              
              ResultSet rs = pstmt.executeQuery();

              while(rs.next()){
			 patientID = rs.getInt("patientID")+1;
                          totalPatient = patientID;      
                }

           int res = pstmt.executeUpdate("INSERT into patient(patientID, pLastName, pFirstName, pMiddleName, pAddress, pCivilStat, pBirthMonth, pBirthDay, pContactNo, pBirthYear, pGender) "
                   + "values ("+patientID+","+lastName+","+firstName+","+middleName+", "+address+", "+civil+", "+birthMonth+", "+birthDay+", "+contactNo+", "+birthYear+", "+gender+")");

			//stat.close();
		 	//connection.close();
                        conn.close();
                        result = true;
            }catch(Exception e){
                e.printStackTrace();
            }
              
        JOptionPane.showMessageDialog(frame, "You have successfully added a new patient!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
        
        dispose();
        new MainWindow(1).setVisible(true);
              }
              
              else if (errCtr > 0){
              errCtr = 0;
              }
         }
    }//GEN-LAST:event_btnConfirmActionPerformed

private void comboMonthActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboMonthActionPerformed
    int index = comboMonth.getSelectedIndex()+1;
    if(index == 2){
        int year = 0;
        if(txtYear.getText().compareTo("<year>")==0){
            JFrame frame = new JFrame();
            JOptionPane.showMessageDialog(frame, "Please input year first", "NOTICE!", JOptionPane.PLAIN_MESSAGE);
}       else year = Integer.parseInt(txtYear.getText());
        if(year%4==0 && (year%100!=0 || year%400==0) )
            comboDay.setModel(new DefaultComboBoxModel(days29));
       else comboDay.setModel(new DefaultComboBoxModel(days28));
        
    } else if(index == 1 || index == 3 || index == 5 || index == 7 || index == 8 || index == 10 ||index == 12){
        comboDay.setModel(new DefaultComboBoxModel(days31));
    } else 
        comboDay.setModel(new DefaultComboBoxModel(days30));
    
    
}//GEN-LAST:event_comboMonthActionPerformed

private void txtYearPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_txtYearPropertyChange
 
}//GEN-LAST:event_txtYearPropertyChange

private void txtYearInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_txtYearInputMethodTextChanged

}//GEN-LAST:event_txtYearInputMethodTextChanged

private void txtYearMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtYearMouseClicked
    txtYear.setText("");
}//GEN-LAST:event_txtYearMouseClicked

private void txtYearFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtYearFocusGained
    if (txtYear.getText().equals(("<year>"))) {
        txtYear.setText("");
    }
}//GEN-LAST:event_txtYearFocusGained

private void txtYearFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtYearFocusLost
    if (txtYear.getText().equals("")) {
        txtYear.setText("<year>");
    }
}//GEN-LAST:event_txtYearFocusLost

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AddPatientWindow().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnConfirm;
    private javax.swing.JComboBox comboCivil;
    private javax.swing.JComboBox comboDay;
    private javax.swing.JComboBox comboMonth;
    private javax.swing.ButtonGroup groupGender;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton radiobtnFemale;
    private javax.swing.JRadioButton radiobtnMale;
    private javax.swing.JTextField txtAddress;
    private javax.swing.JTextField txtContactNo;
    private javax.swing.JTextField txtFirstName;
    private javax.swing.JTextField txtLastName;
    private javax.swing.JTextField txtMiddleName;
    private javax.swing.JTextField txtYear;
    // End of variables declaration//GEN-END:variables

  
     DateFormat year1 = new SimpleDateFormat("yyyy");
     DateFormat month1 = new SimpleDateFormat("MM");
     DateFormat day1 = new SimpleDateFormat("dd");
        Date now = new Date();
 
    
        public int patientCheck(String firstName, String lastName, String middleName,String address,
                String birthMonth, String civil, String contactNo, String birthYear, int day, String gender){
           
            JFrame frame = new JFrame(); boolean exists=false;
            int ret = 0;
              try{
                  
                  
              ConnectionFactory myFactory = ConnectionFactory.getFactory();
              Connection conn = myFactory.getConnection();
 
            
            
              PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM dcds.patient WHERE pFirstName = "+firstName
                      +"AND pMiddleName = "+middleName+ "AND pLastName = " +lastName);
              
              
              ResultSet rs = pstmt.executeQuery();
              int count = 0;
              while(rs.next() && exists==false){
            
                //  String addr = "\""+rs.getString("pAddress") +"\"";
                  String month = "\"" + rs.getString("pBirthMonth") + "\"";
                //  String civ = "\""+rs.getString("pCivilStat")+"\"";
                //  String contact = "\""+ rs.getString("pContactNo")+"\"";
                  String year = "\""+rs.getString("pBirthYear")+"\"";
                //  String gen = "\""+rs.getString("pGender") +"\"";
                  int bday = rs.getInt("pBirthDay");
                  
                  if(day == bday && year.compareToIgnoreCase(birthYear)==0 && month.compareToIgnoreCase(birthMonth)==0){
                       ret = 1;exists = true;
                       JOptionPane.showMessageDialog(frame, "Patient already exists!", "NOTICE!", JOptionPane.ERROR_MESSAGE);
                       
                  
                  }
             }
                        conn.close();
                      
            }catch(Exception e){
                e.printStackTrace();
            }
       
           return ret;   
        }
        
}
